package com.hh.game.client;

import com.hh.game.service.gateway.codec.websocket.InputMessage;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.ReferenceCountUtil;
import org.java_websocket.client.DefaultSSLWebSocketClientFactory;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.concurrent.CountDownLatch;

public class SocketClient extends WebSocketClient {
    private SockectOnConnctionMsg msg;
    private CountDownLatch latch;

    public SocketClient(String uri, CountDownLatch latch, SockectOnConnctionMsg msg) throws Exception {
        super(new URI(uri));
        this.msg = msg;
        if (uri.indexOf("wss") == 0) {
            trustAllHosts(this);
        }
        this.latch = latch;

        this.connectBlocking();
    }


    public void send() {
        super.send(msg.getMsg());
    }

    @Override
    public void onOpen(ServerHandshake arg0) {

    }

    private void trustAllHosts(SocketClient appClient) {
        appClient.setWebSocketFactory(getSslWebSock());
    }

    private DefaultSSLWebSocketClientFactory getSslWebSock() {
        DefaultSSLWebSocketClientFactory sslWebSock = null;
        TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return new java.security.cert.X509Certificate[]{};
            }

            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
                    throws java.security.cert.CertificateException {

            }

            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
                    throws java.security.cert.CertificateException {
            }
        }};
        try {
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            sslWebSock = new DefaultSSLWebSocketClientFactory(sc);
        } catch (Exception e) {
        }
        return sslWebSock;
    }

    @Override
    public void onMessage(ByteBuffer bytes) {
        ByteBuf buf = Unpooled.copiedBuffer(bytes);
        InputMessage in = InputMessage.buildInputMessage(buf);
        System.out.println(in);
        ReferenceCountUtil.release(buf);
        latch.countDown();
    }

    @Override
    public void onClose(int arg0, String arg1, boolean arg2) {
        System.out.println("------------ onClose --------------");
        latch.countDown();
    }

    @Override
    public void onError(Exception arg0) {
        arg0.printStackTrace();
        close();
        latch.countDown();
    }

    @Override
    public void onMessage(String arg0) {
        System.out.println(arg0);
    }
}
